<?php


namespace app\admin\controller;


use app\admin\validate\HotelValidate;
use app\BaseController;
use app\model\Hotel;
use app\service\FilesService;
use app\service\HotelService;
use think\App;
use think\facade\Request;

class HotelController extends BaseController
{
    private $service;
    private $FilesService;

    public function __construct(App $app)
    {
        $this->service = new HotelService();
        $this->FilesService = new FilesService();
        parent::__construct($app);
    }

    public function getHotel(Hotel $hotel){
        $result = $this->service->getHotel($hotel->id);
        return $this->returnJson($result);
    }

    public function getHotelList(){
        $param = $this->request->param(['page' => 1 , 'limit' => 10]);
        $keyword = $this->request->param('keyword', '');
        $province = $this->request->param('province', '');
        $city = $this->request->param('city', '');

        $result = $this->service->getHotelList(
            $param['page'], $param['limit'], $keyword, $province, $city
        );

        return $this->returnJson($result);
    }

    public function editHotel(Hotel $hotel){
        $param = Request::param(['province', 'city', 'district', 'address', 'phone', 'wechat' , 'name']);
        $param['img_urls'] = $this->request->param('img_urls');
        $param['logo_url'] = $this->request->param('logo_url');

        return $this->returnJson($this->service->editHotel($hotel->id, $param));
    }

    public function addHotel(){
        $param = Request::post(['name' , 'province' , 'city' , 'district' , 'address' , 'linkman' , 'phone' , 'wechat']);
        $param['img_urls'] = $this->request->param('img_urls');
        $param['logo_url'] = $this->request->param('logo_url');

        validate(HotelValidate::class)
            ->scene('addHotel')
            ->check($param);

        $data = $this->service->addHotel($param['name'] , $param['province'] , $param['city'] , $param['district'] , $param['address'] , $param['linkman'] , $param['phone'] , $param['wechat'] ,  $param['img_urls'] , $param['logo_url']);
        return $this->returnJson($data);
    }

    public function banHotel(Hotel $hotel){
        $is_ban =(int) $this->request->param('is_ban' , 1);

        return $this->returnJson($this->service->banHotel($is_ban, $hotel->id));
    }
}